//
//  MyPraiseViewController.m
//  used
//
//  Created by lanou on 16/7/20.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "MyPraiseViewController.h"

@interface MyPraiseViewController ()


@property (nonatomic, strong) NSMutableArray *models;

@end

@implementation MyPraiseViewController

- (NSMutableArray *)models {
    if (!_models) {
        _models = [NSMutableArray array];
    }
    return _models;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setNavigation];
    [self setTableView];
    
    
    [self loadPraiseData];
    
}

- (void)setTableView {

    self.tableView.backgroundColor = KLRGBColor(240, 240, 240);
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"KLMyPraiseCell" bundle:nil] forCellReuseIdentifier:@"praiseCell"];

}

- (void)setNavigation
{
    self.navigationItem.title = @"我赞过的";
    self.view.backgroundColor = KLRGBColor(240, 240, 240);
    
}

- (void)loadPraiseData {
    
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        BmobQuery *query = [BmobQuery queryWithClassName:@"Zan"];
        [query whereKey:@"user" equalTo:[BmobUser getCurrentUser]];
        [query orderByDescending:@"updatedAt"];
        [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            
            for (BmobObject *obj in array) {
                
                KLCommodity *goodModel = [[KLCommodity alloc] init];
    
                goodModel.productname = [obj objectForKey:@"productname"];
                goodModel.saleprice = [obj objectForKey:@"saleprice"];
                goodModel.desc = [obj objectForKey:@"desc"];
                goodModel.productImage = [obj objectForKey:@"image"];
                goodModel.ID = [obj objectForKey:@"productID"];

                [self.models addObject:goodModel];

            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }];
    });
    
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.models.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 150;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KLMyPraiseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"praiseCell"];
    cell.model = self.models[indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    YSLCommodityViewController *commodityVC = [[YSLCommodityViewController alloc] init];
    KLGood *model = self.models[indexPath.row];
    //model传值CommodityModel
    commodityVC.goodsID = model.ID;
    
    [self.navigationController pushViewController:commodityVC animated:YES];
    
}


@end
